/*
  年月日联动下拉选择JS封装类 Ver 1.0 版
  制作时间:2013-3-12
　更新时间:2013-3-12
  应用说明:页面包含<script type="text/javascript" src="assets/js/custom/client/YMDClass.js" charset="utf-8"></script>
  <select name="year1"></select>
  <select name="month1"></select>
  <select name="day1"></select>
  <script>
  new YMDselect('year1','month1','day1',1990,2,10);
  </script>
	年月联动
	  new YMDselect('year1','month1');
	  new YMDselect('year1','month1',1990);
	  new YMDselect('year1','month1',1990,2);
	年月日联动
	  new YMDselect('year1','month1','day1');
	  new YMDselect('year1','month1','day1',1990);
	  new YMDselect('year1','month1','day1',1990,2);
	  new YMDselect('year1','month1','day1',1990,2,10);
	  */
var SYT = "-请选择年份-";
var SMT = "-请选择月份-";
var SDT = "-请选择日期-";
var BYN = 50;//年份范围往前50年
var AYN = 5;//年份范围往后0年
function YMDselect() {
    var yArr = document.getElementsByName(arguments[0]);
    var mArr = document.getElementsByName(arguments[1]);
    var dArr = document.getElementsByName(arguments[2]);
    this.year = yArr[0];
    this.month = mArr[0];
    this.day = dArr[0];
    this.defaultYear = this.day ? arguments[3] : arguments[2];
    this.defaultMonth = this.day ? arguments[4] : arguments[3];
    this.defaultDay = this.day ? arguments[5] : arguments[4];
    this.year.YMD = this;
    this.month.YMD = this;
    this.year.onchange = function () {
        YMDselect.SetM(this.YMD)
    };
    if (this.day) this.month.onchange = function () {
        YMDselect.SetD(this.YMD)
    };
    YMDselect.SetY(this);
    this.val = function () {
        var convert = function (val) {
            return val < 10 ? ("0" + val) : val;
        };
        return convert(this.year.value) + "-" + convert(this.month.value) + "-" + convert(this.day.value);
    }
    return this;
};
//设置年份
YMDselect.SetY = function (YMD) {
    var dDate = new Date();
    var  dCurYear = dDate.getFullYear();
    YMD.year.options.add(new Option(SYT, '0'));
    for (i = dCurYear + AYN; i > (dCurYear - BYN); i--) {
        YMDYT = i + '年';
        YMDYV = i;
        OptY = new Option(YMDYT, YMDYV);
        YMD.year.options.add(OptY);
        if (YMD.defaultYear == YMDYV) OptY.selected = true
    }
    YMDselect.SetM(YMD)
};
//设置月份
YMDselect.SetM = function (YMD) {
    YMD.month.length = 0;
    YMD.month.options.add(new Option(SMT, '0'));
    if (YMD.year.value > 0) {
        for (var i = 1; i <= 12; i++) {
            YMDMT = i + '月';
            YMDMV = i;
            OptM = new Option(YMDMT, YMDMV);
            YMD.month.options.add(OptM);
            if (YMD.defaultMonth == YMDMV) OptM.selected = true
        }
    }
    if (YMD.day) YMDselect.SetD(YMD)
};
//设置日期
YMDselect.SetD = function (YMD) {
    YI = YMD.year.value;
    MI = YMD.month.value;
    YMD.day.length = 0;
    YMD.day.options.add(new Option(SDT, '0'));
    if (YI > 0 && MI > 0) {
        dPrevDate = new Date(YI, MI, 0);
        daysInMonth = dPrevDate.getDate();
        for (d = 1; d <= parseInt(daysInMonth); d++) {
            YMDDT = d + '日';
            YMDDV = d;
            OptD = new Option(YMDDT, YMDDV);
            YMD.day.options.add(OptD);
            if (YMD.defaultDay == YMDDV) OptD.selected = true
        }
    }
}